package maven.jyhs.com.controller;

import maven.jyhs.com.dao.ApiResult;
import maven.jyhs.com.entity.User;
import maven.jyhs.com.entity.dto.Users;
import maven.jyhs.com.servce.TranstfeSevrce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TranstfeConetroller {
    @Autowired
    private TranstfeSevrce transtfeService;

    @GetMapping("/transfer")
    public Map<String, Object> transferSelect(Users  user) throws SQLException, ClassNotFoundException, IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count",transtfeService.limit(user));
        map.put("data",  transtfeService.getUsers(user));
        return map;

    }

    @PostMapping("/transfer")
    public ApiResult transferAdd(@Valid @RequestBody User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder sb = new StringBuilder();
            bindingResult.getAllErrors().stream().forEach(error -> {
                System.out.println(error.getDefaultMessage());   //输出错误信息
                sb.append(error.getDefaultMessage());
            });
            return new ApiResult(400,sb.toString() , null);
        }
        System.out.println("添加用户");
        return new ApiResult(200, "添加成功", transtfeService.add(user));
    }
    @PostMapping("/transfer/updateImage")
    public ApiResult updateMenuImage(List<MultipartFile> file) throws IOException {
        List<String> fileUrls = new ArrayList<>();


        for (MultipartFile files : file) {
            String newFileName = System.currentTimeMillis() + "_" + files.getOriginalFilename();
            files.transferTo(new File("D:/image/" + newFileName));
            fileUrls.add("http://localhost:8080/image/" + newFileName);
        }
        return new ApiResult(200, "上传成功", fileUrls);

    }
    @PutMapping("/transfer")
    public ApiResult transferUpdate(@Valid @RequestBody User user, BindingResult bindingResult )  {
        if (bindingResult.hasErrors()) {
            StringBuilder sb = new StringBuilder();
            bindingResult.getAllErrors().stream().forEach(error -> {
                System.out.println(error.getDefaultMessage());   //输出错误信息
                sb.append(error.getDefaultMessage());
            });
            return new ApiResult(400,sb.toString() , null);
        }
        System.out.println("修改用户");
//        user.setId(id);
        return new ApiResult(200, "更新成功", transtfeService.update(user));
    }


    @DeleteMapping("/transfer/{id}")
    public ApiResult transferDelete(@PathVariable int id) throws IllegalAccessException {
        System.out.println("删除用户");
        return new ApiResult(200, "删除成功", transtfeService.delete(id));
    }



}
